import PageBody from '@/components/school/pageBody';

const app = getApp();
import diyJson from '@/config/diy.js';
import {mapGetters} from "vuex";
import * as  StudentApi from '@/api/member/student.js';

export const mixins = {
  components: {
    PageBody
  }
  , computed: mapGetters(['userInfo', 'schoolId', 'isLogin']), data() {
    return {
      themConfig: diyJson.commonStyle
      , scrollTop: 0
      , showLoading: true
      , datas: []
      , schoolName: '学校筛选'
      , schools: []
      , pageBody: {

      }
      , isReachBottom: false
      , loading: {
        status: 'nomore',
        loadingText: '加载中',
        loadmoreText: '点击加载更多'
        , dataPage: {
          current: 1,
          pages: 1,
          size: 10,
        }
      }
      , searchParams: {
        state: null
      }
      , dataPage: {
        current: 1,
        pages: 1,
        size: 10,
        total: 10,
      }
      , tabCurrent: 0
      , tabList: [
        {
          name: "全部",
          value: undefined,
        },
        {
          name: "待审核",
          value: 0,
        },
        {
          name: "审核通过",
          value: 1,
        },
        {
          name: "不通过",
          value: 2,
        }
      ]
      ,form:{
        state: undefined,
        id: undefined,
        refuseReason:''
      }
    }
  }
  , onShow() {
    this.getSchoolData()
    this.dataList({})
  },
  mounted() {
    this.computeAmountHeight()
  },
  onPullDownRefresh() {
    this.dataList({...this.searchParams});
    uni.stopPullDownRefresh();
  },
  onReachBottom() {
    this.scrolltolower();
  },
  methods: {
    async computeAmountHeight() {
      // const query = uni.createSelectorQuery().in(this);
      // let that = this;
      // query.select('.heightWrapper')
      // .boundingClientRect((dome) => {
      //   // console.log("height", dome.height)
      //   that.pageBody['paddingTop'] = dome.height;
      // })
      // .exec();
    },

    // 选择学校
    openSchool() {
      this.$refs.actionSheet.open();
    },

    // 选择学校
    selectSchool(e) {
      if (e.name === '清空') {
        delete this.searchParams['schoolIds'];
        this.schoolName = '学校筛选';
      } else {
        this.schoolName = e.name;
        this.searchParams['schoolIds'] = [e.id];
      }
      this.dataList({...this.searchParams});
    },

    // 图片放大
    imgOpenLager(e) {
      uni.previewImage({
        urls: [e], //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
        current: '', // 当前显示图片的http链接，默认是第一个
        success: function (res) {
        },
        fail: function (res) {
        },
        complete: function (res) {
        },
      })
    },

    // 状态切换
    tabListChange(e) {
      this.searchParams.state = e.value
      this.loading.status = 'nomore';
      this.dataList({...this.searchParams});
    },
    // 滚到底部
    scrolltolower() {
      this.isReachBottom = true;
      this.loading.status = 'nomore';
      if (this.dataPage.current < this.dataPage.pages) {
        this.loading.status = 'loadmore';
      }
    },
    loadmore() {
      if (this.dataPage.current < this.dataPage.pages) {
        this.loading.status = 'loading';
        StudentApi.pages(
            {...this.searchParams, current: this.dataPage.current + 1}).then(
            res => {
              res.data.records.forEach(item => {
                this.datas.push(item)
              })
              this.dataPage.current = res.data.current;
              this.dataPage.pages = res.data.pages;
              this.loading.status = 'loadmore';
            })
      }
    },
    refreshList() {
      this.dataList({...this.searchParams});
    },

    // 获取搜索学校列表
    getSchoolData() {
      StudentApi.getSchools().then(res => {
        let arr = [{name:'清空',id:''}];
        res.data.forEach(item => {
          arr.push({name: item.schoolName, id: item.id})
        })
        this.schools = arr;
      })
    },

    dataList(params) {
      if (this.datas.length > 0) {
        this.datas.splice(0, 1);
      }
      StudentApi.pages(params).then(res => {
        this.datas = res.data.records;
        this.dataPage.current = res.data.current;
        this.dataPage.total = res.data.total;
        this.dataPage.pages = res.data.pages;
      })
    },
    submit(pass,item){
      this.form.state = pass?1:2;
      this.form.id = item.id;
      this.$refs.popup.open();
    },
    closeForm(){
      this.$refs.popup.close();
      this.form={
        state: undefined,
        id: undefined,
        refuseReason:''
      }
    },
    submitForm(){
      if (this.form.state===2 && this.form.refuseReason ==='' ){
        return;
      }
      StudentApi.authAudit(this.form).then(res=>{
        this.closeForm();
        this.dataList({...this.searchParams})
      })
    }
  }
}
